home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Human Interface Toolbox / Tabs LDEF / Windows.c < prev   
Encoding:
C/C++ Source or Header  |  2000-09-28  |  8.9 KB  |  374 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Windows.c
  3.  
  4.     Contains:    Handle application's windows
  5.  
  6.     Written by: Chris White    
  7.  
  8.     Copyright:    Copyright Â© 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/10/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24.  
  25. #pragma segment Core
  26.  
  27.  
  28. #include <Sound.h>
  29. // System Includes
  30.  
  31. #ifndef __TYPES__
  32.     #include <Types.h>
  33. #endif
  34.  
  35. #ifndef __WINDOWS__
  36.     #include <Windows.h>
  37. #endif
  38.  
  39. #ifndef __QUICKDRAW__
  40.     #include <Quickdraw.h>
  41. #endif
  42.  
  43. #ifndef __RESOURCES__
  44.     #include <Resources.h>
  45. #endif
  46.  
  47. #ifndef __FONTS__
  48.     #include <Fonts.h>
  49. #endif
  50.  
  51. #ifndef __ERRORS__
  52.     #include <Errors.h>
  53. #endif
  54.  
  55.  
  56.  
  57.  
  58. // Application Includes
  59.  
  60. #ifndef __BAREBONES__
  61.     #include "BareBones.h"
  62. #endif
  63.  
  64. #ifndef __PROTOTYPES__
  65.     #include "Prototypes.h"
  66. #endif
  67.  
  68.  
  69.  
  70.  
  71. // Static prototypes
  72.  
  73. static OSErr    CreateContentList ( WindowRef theWindow, tContentsProcPtr contentsProc, void* refCon );
  74. static OSErr    AddListContents ( ListRef theList, void* refCon );
  75.  
  76.  
  77. OSErr            PatchListLDEF ( ListRef theList );
  78.  
  79.  
  80.  
  81.  
  82.  
  83. //
  84. // This is called to create the application's window.
  85. //
  86. void CreateWindow ( void )
  87. {
  88.     OSErr        theErr;
  89.     WindowRef    theWindow;
  90.     
  91.     theErr = CreateListWindow ( &theWindow, AddListContents, nil );
  92.     if ( theErr )
  93.         AlertUser ( kGenericErrorStr, theErr, "\p" );
  94.     
  95.     return;
  96. }
  97.  
  98.  
  99.  
  100. //
  101. // This will close the application's window, dispose of any storage we've hung
  102. // off the window, and then dispose of the window itself.
  103. //
  104. WindowRef DestroyWindow ( WindowRef windowRef )
  105. {
  106.     if ( windowRef )
  107.     {
  108.         ListRef theList;
  109.         
  110.         theList = (ListRef) GetWRefCon ( windowRef );
  111.         if ( theList )
  112.             LDispose ( theList );
  113.         
  114.         DisposeWindow ( windowRef );
  115.         
  116.     }
  117.     
  118.     return nil;
  119. }
  120.  
  121.  
  122.  
  123. //
  124. // Creates a window with a list in the centre. contentsProc is a routine
  125. // pointer to intitialize the list. It's passed the refCon to assist.
  126. //
  127. OSErr CreateListWindow ( WindowRef* windowRef, tContentsProcPtr contentsProc, void* refCon )
  128. {
  129.     OSErr        theErr;
  130.     WindowRef    theWindow;
  131.     
  132.     
  133.     theWindow = GetNewCWindow ( kDisplayWindow, nil, (WindowRef) -1 );
  134.     if ( theWindow == nil )
  135.         return (ResError ( )) ? ResError ( ) : resNotFound;
  136.     
  137.     SetPortWindowPort ( theWindow );
  138.     TextFont ( kFontIDGeneva );
  139.     TextFace ( normal );
  140.     TextSize ( 9 );
  141.     
  142.      theErr = CreateContentList ( theWindow, contentsProc, refCon );
  143.     if ( theErr )    goto CleanupAndBail;
  144.     
  145.     SelectWindow ( theWindow );
  146.     ShowWindow ( theWindow );
  147.     
  148.     *windowRef = theWindow;
  149.     
  150.     return noErr;
  151.     
  152.  
  153. CleanupAndBail:
  154.     
  155.     // Don't forget to free any storage we've used so far
  156.     DestroyWindow ( theWindow );
  157.     
  158.     return theErr;
  159. }
  160.  
  161.  
  162.  
  163. //
  164. // This is passed into the CreateDocumentWindow routine
  165. // to add the contents to the newly created list.
  166. //
  167. static OSErr AddListContents ( ListRef theList, void* refCon )
  168. {
  169.     #pragma unused(refCon)
  170.     AddToList ( theList, "\pApple Extras\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  171.     AddToList ( theList, "\pApplications\t-\tfolder\tWed, Dec 18, 1995, 12:04 pm" );
  172.     AddToList ( theList, "\pClean System Folder\t-\tfolder\tThu, Dec 7, 1995, 1:46 pm" );
  173.     AddToList ( theList, "\pCommunications\t-\tfolder\tWed, Dec 6, 1995, 2:21 pm" );
  174.     AddToList ( theList, "\pCopland Stuff\t-\tfolder\tTue, Aug 29, 1995, 1:14 pm" );
  175.     AddToList ( theList, "\pDevelopment\t-\tfolder\tWed, Dec 13, 1995, 4:27 pm" );
  176.     AddToList ( theList, "\pDocumentation\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  177.     AddToList ( theList, "\pDTS\t-\tfolder\tWed, Dec 18, 1995, 1:07 pm" );
  178.     AddToList ( theList, "\pMisc\t-\tfolder\tMon, Dec 16, 1995, 8:56 pm" );
  179.     AddToList ( theList, "\pProjects\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  180.     AddToList ( theList, "\pStress\t-\tfolder\tTue, Aug 29, 1995, 1:14 pm" );
  181.     AddToList ( theList, "\pSystem Folder\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  182.     AddToList ( theList, "\pSystem Installers\t-\tfolder\tWed, Dec 6, 1995, 2:21 pm" );
  183.     AddToList ( theList, "\pSystem Picker\t-\tapplication program\tMon, Dec 16, 1995, 8:56 pm" );
  184.     AddToList ( theList, "\pTools\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  185.     AddToList ( theList, "\pAppleScriptâ„¢ Utilities\t-\tfolder\tThu, Dec 7, 1995, 12:46 pm" );
  186.     AddToList ( theList, "\pDebuggers\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  187.     AddToList ( theList, "\pDebugging Modern Memory Manager\t-\tfolder\tMon, Dec 16, 1995, 8:56 pm" );
  188.     AddToList ( theList, "\pDebugging OD Part Editors\t33K\tSimpleText document\tThu, Dec 19, 1995, 12:07 pm" );
  189.     AddToList ( theList, "\pDocumentation Viewers\t-\tfolder\tMon, Dec 16, 1995, 12:56 pm" );
  190.     AddToList ( theList, "\pPartMaker 4.3\t-\tfolder\tMon, Dec 16, 1995, 8:56 pm" );
  191.     AddToList ( theList, "\pReference\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  192.     AddToList ( theList, "\pResource Editors\t5,049K\tfolder\tWed, Dec 6, 1995, 2:21 pm" );
  193.     AddToList ( theList, "\pSDK\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  194.     AddToList ( theList, "\pSoundMgr3.2Dev.sea\t198K\tapplication program\tThu, Dec 19, 1995, 12:07 pm" );
  195.     
  196.     
  197.     return noErr;
  198.     
  199. } // AddContents
  200.  
  201.  
  202.  
  203.  
  204. //
  205. // This is called to create the List Manager list, and initialize it.
  206. //
  207. static OSErr CreateContentList ( WindowRef theWindow, tContentsProcPtr contentsProc, void* refCon )
  208. {
  209.     OSErr        theErr;
  210.     ListHandle    theList;
  211.     Point        cellSize = {0, 0};
  212.     Cell        firstCell = {0, 0};
  213.     Rect         dataRect = {0, 0, 0, 1};
  214.     Rect        viewRect;
  215.     
  216.     
  217.     // The width of a cell needs to be 32767 so any hiliting
  218.     // doesn't stop short of the window width after it's been resized.
  219.     
  220.     viewRect = theWindow->portRect;
  221.     InsetRect ( &viewRect, 20, 20 );
  222.     viewRect.right -= 15;                // minus scrollbar width
  223.     theList = LNew ( &viewRect, &dataRect, cellSize, kLDEFID, theWindow, 
  224.                         false, false, false, true );
  225.  
  226.     #ifndef USE_LDEF
  227.         theErr = PatchListLDEF ( theList );
  228.         if ( theErr )
  229.             return theErr;
  230.     #endif
  231.     
  232.     if ( theList )
  233.     {
  234.         static short    theOffsets[] = { 4, 0, 100, 134, 200 };
  235.         
  236.         (*theList)->refCon = (long) &theOffsets;
  237.         (*theList)->selFlags = lOnlyOne;
  238.         SetWRefCon ( theWindow, (long) theList );
  239.         
  240.         // Since the calling routine is always in the same architecture type as
  241.         // the callback routine, we don't need to worry about any Mixed Mode
  242.         // complications. We just treat it as a straight forward routine pointer.
  243.         if ( contentsProc )
  244.         {
  245.             theErr = (*contentsProc) ( theList, refCon );
  246.             if ( theErr )
  247.                 return theErr;
  248.         }
  249.         
  250.         LSetSelect ( true, firstCell, theList );
  251.         
  252.         // Now the list has been fully prepared, turn the drawing mode on
  253.         LSetDrawingMode ( true, theList );
  254.     }
  255.     
  256.     return noErr;
  257.     
  258. } // CreateContentList
  259.  
  260.  
  261.  
  262. void DoActivate ( EventRecord* theEvent )
  263. {
  264.     Boolean        bActiveFlag = theEvent->modifiers & resumeFlag;
  265.     GrafPtr        savePort;
  266.     WindowRef    theWindow = (WindowRef) theEvent->message;
  267.     
  268.     
  269.     GetPort ( &savePort );
  270.     SetPortWindowPort ( theWindow );
  271.     LActivate ( bActiveFlag, (ListRef) GetWRefCon ( theWindow ) );
  272.     SetPort ( savePort );
  273.     
  274.     return;
  275. }
  276.  
  277.  
  278.  
  279. void DoUpdate ( EventRecord* theEvent )
  280. {
  281.     GrafPtr            savePort;
  282.     CGrafPtr        thePort;
  283.     WindowRef        theWindow = (WindowRef) theEvent->message;
  284.     ListRef            theList = nil;
  285.     Rect            theRect;
  286.     
  287.     
  288.     
  289.     thePort = GetWindowPort ( theWindow );
  290.     
  291.     GetPort ( &savePort );
  292.     SetPortWindowPort ( theWindow );
  293.     BeginUpdate ( theWindow );                    // visRgn temporarily = updateRgn
  294.     EraseRect ( &thePort->portRect );
  295.     
  296.     theList = (ListRef) GetWRefCon ( theWindow );
  297.     if ( theList )
  298.     {
  299.         PenNormal ( );
  300.         GetListRect ( theList, &theRect );
  301.         InsetRect ( &theRect, -1, -1 );
  302.         FrameRect ( &theRect );
  303.         
  304.         LUpdate ( thePort->visRgn, theList );
  305.     }
  306.     
  307.     EndUpdate ( theWindow );                    // restore normal visRgn of grafport
  308.     SetPort ( savePort );
  309.     
  310.     return;
  311. }
  312.  
  313.  
  314.  
  315. void DoContentClick ( WindowRef theWindow, EventRecord* theEvent )
  316. {
  317.     WindowRef    frontWindow;
  318.     
  319.     // If a movable modal is active, ignore click in an inactive 
  320.     // window, otherwise select it or handle the content click.
  321.     
  322.     frontWindow = FrontWindow ( );
  323.     if ( theWindow != frontWindow )
  324.     {
  325.         if ( IsMovableModal ( frontWindow ) )
  326.             SysBeep ( 30 );
  327.         else
  328.             SelectWindow ( theWindow );
  329.     }
  330.     else
  331.     {
  332.         GrafPtr        savePort;
  333.         
  334.         GetPort ( &savePort );
  335.         SetPortWindowPort ( theWindow );
  336.         HandleListClick ( theWindow, theEvent );
  337.         SetPort ( savePort );
  338.     }
  339.     
  340.     return;
  341.     
  342. } // DoContentClick
  343.  
  344.  
  345.  
  346. void DoDragWindow ( WindowRef theWindow, EventRecord* theEvent )
  347. {
  348.     WindowRef    frontWindow;
  349.     
  350.     
  351.     // If a movable modal is active, ignore click in an inactive 
  352.     // title bar, otherwise let the Window Manager handle it.
  353.     
  354.     frontWindow = FrontWindow ( );
  355.     if ( theWindow != frontWindow && IsMovableModal ( frontWindow ) )
  356.         SysBeep ( 30 );
  357.     else                                
  358.     {
  359.         RgnHandle    theRgn;
  360.         Rect        dragRect;
  361.         
  362.         theRgn = GetGrayRgn ( );
  363.         dragRect = (*theRgn)->rgnBBox;
  364.         DragWindow ( theWindow, theEvent->where, &dragRect );
  365.     }
  366.     
  367.     return;
  368. }
  369.  
  370.  
  371.  
  372.  
  373.  
  374.